<?php

namespace app\merchant\controller\market;

use app\merchant\controller\Base;
use app\merchant\model\Coupon as CouponModel;
use app\merchant\model\UserCoupon as UserCouponModel;

/**
 * 优惠券管理
 * Class Coupon
 * @package app\store\controller\market
 */
class Coupon extends Base
{
    /* @var CouponModel $model */
    private $model;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new CouponModel;
    }

    /**
     * 优惠券列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $list = $this->model->getList();
        return $this->renderSuccess($list,'查询成功');
    }


    /**
     * 优惠券详情
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        if(!isset($param['coupon_id']) || isInt($param['coupon_id'])){
            return $this->renderError([],'参数错误');
        }
        // 优惠券详情
        $model = CouponModel::detail($param['coupon_id']);
        if(!$model){
            return $this->renderError([],'优惠券不存在，请刷新重试');
        }

        return $this->renderSuccess($model,'查询成功');
    }


    /**
     * 添加优惠券
     * @return array|mixed
     */
    public function add()
    {
        $param = $this->postData();
        // 新增记录
        if ($this->model->add($param)) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$this->model->getError() ?: '添加失败');
    }

    /**
     * 更新优惠券
     * @param $coupon_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->postData();
        if(!isset($param['coupon_id']) || isZeroNumeric($param['coupon_id'])){
            return $this->renderError([],"参数错误");
        }
        // 优惠券详情
        $model = CouponModel::detail($param['coupon_id']);
        if(!$model){
            return $this->renderError([],"优惠券不存在，请刷新重试");
        }

        // 更新记录
        if ($model->edit($param)) {
            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError([],$model->getError() ?: '编辑失败');
    }

    /**
     * 删除优惠券
     * @param $coupon_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        if(!isset($param['coupon_id']) || isZeroNumeric($param['coupon_id'])){
            return $this->renderError([],"参数错误");
        }
        // 优惠券详情
        $model = CouponModel::detail($param['coupon_id']);
        if(!$model){
            return $this->renderError([],"优惠券不存在，请刷新重试");
        }

        // 更新记录
        if ($model->setDelete()) {
            return $this->renderSuccess([],'删除成功');
        }
        return $this->renderError([],$model->getError() ?: '删除成功');
    }

    /**
     * 领取记录
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function receive()
    {
        $model = new UserCouponModel;
        $list = $model->getList($this->postData());
        return $this->renderSuccess($list, "查询成功");
    }

}